package com.av.recorder.audiorecord;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import com.av.recorder.audiorecord.AudioManager.AudioStageListener;
import java.io.File;

public class AudioRecordUtils {
    private static final int STATE_NORMAL = 1;
    private static final int STATE_RECORDING = 2;
    private static final int STATE_WANT_TO_CANCEL = 3;
    private static final int DISTANCE_Y_CANCEL = 50;
    private int mCurrentState = 1;
    private boolean isRecording = false;
    private AudioManager mAudioManager;
    private float mTime = 0.0F;
    private int maxTime;
    private boolean mReady;
    private long startTime;
    public boolean send_first = true;
    Context context;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch(msg.what) {
                case 0:
                    if (AudioRecordUtils.this.maxTime > 0) {
                        AudioRecordUtils.this.maxTime--;
                        msg.getTarget().sendEmptyMessageDelayed(0, 1000L);
                    } else {
                        AudioRecordUtils.this.finishRecord(true);
                    }
                break;
            }
            return false;
        }
    }) ;
    private AudioRecordUtils.AudioFinishRecorderListener mListener;
    AudioStageListener stageListener = new AudioStageListener() {
        public void wellPrepared() {
        }
    };

    public AudioRecordUtils(Context context, int maxTime) {
        this.context = context;
        this.maxTime = maxTime;
        String dir = Environment.getExternalStorageDirectory() + "/recorder_audios";
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }

        this.mAudioManager = AudioManager.getInstance(dir);
        this.mAudioManager.setOnAudioStageListener(this.stageListener);
    }

    public void startReconrd() {
        this.mReady = true;
        this.startTime = System.currentTimeMillis();
        if (this.mAudioManager.prepareAudio()) {
            this.handler.sendEmptyMessage(0);
        }

    }

    public void setAudioFinishRecorderListener(AudioRecordUtils.AudioFinishRecorderListener listener) {
        this.mListener = listener;
    }

    public void finishRecord(boolean isAutoFinish) {
        this.handler.removeMessages(0);
        this.mAudioManager.release();
        this.send_first = true;
        if (this.mListener != null) {
            this.mListener.onFinished((float)((System.currentTimeMillis() - this.startTime) / 1000L), this.mAudioManager.getCurrentFilePath(), isAutoFinish);
        }

        this.reset();
    }

    private void reset() {
        this.isRecording = false;
        this.changeState(1);
        this.mReady = false;
        this.mTime = 0.0F;
    }

    private void changeState(int state) {
        if (this.mCurrentState != state) {
            this.mCurrentState = state;
            switch(this.mCurrentState) {
                case 1:
                case 3:
                default:
                    break;
                case 2:
                    if (this.isRecording) {
                    }
            }
        }

    }

    public interface AudioFinishRecorderListener {
        void onFinished(float var1, String var2, boolean var3);
    }
}

